package edu.progra2.ventas.action;

import javax.persistence.*;
import org.apache.struts2.convention.annotation.*;

import edu.progra2.common.util.Utilitario;
import edu.progra2.ventas.model.User;


@Action(value="login",
results={
	@Result(name="exitoadm",location="/menu-adm.jsp"),
	@Result(name="exitocli",location="/menu-cli.jsp"),
	@Result(name="exitorec",location="/menu-rec.jsp"),
	@Result(name="exitoalm",location="/menu-alm.jsp"),
	@Result(name="error",location="/login.jsp")
} )
public class LoginAction {
	private User user;
	private String message;
	
	public String execute(){
		String result="exito";
		EntityManager em=
				Utilitario.getInstance().getEntityManager();
		User userdb=em.find(User.class, user.getUsuario());
		
		
		
		if(userdb!=null){
			if(userdb.getPassword().equals(user.getPassword())){
				user.setAlias(userdb.getAlias());
				
					if(userdb.getCargo().equals("A")){
						result="exitoadm";
					}else if(userdb.getCargo().equals("C")){
						result="exitocli";
					}else if(userdb.getCargo().equals("R")){
						result="exitorec";
					}else{
							result="exitoalm";
					}
				
				
				
			}else{
				setMessage("Password does not match");
				result="error";
			}
		}else{
			setMessage("User does not exists");
			result="error";
		}  
		return result;
	}

	
	
	public User getUser() {
		return user;
	}

	public void setUser(User user) {
		this.user = user;
	}

	public String getMessage() {
		return message;
	}

	public void setMessage(String message) {
		this.message = message;
	}
	
	
}
